<?php

use yii\db\Migration;
use common\components\RbacManager;

/**
 * Handles the creation of table `{{%system_menu}}`.
 */
class m230831_092434_create_system_menu_table extends Migration
{
    const TABLE_NAME = '{{%system_menu}}';

    /**
     * {@inheritdoc}
     */
    public function up()
    {
        $tableOptions = null;
        if ($this->db->driverName === 'mysql') {
            $tableOptions = 'CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci ENGINE=InnoDB COMMENT="系统菜单表"';
        }

        $this->createTable(self::TABLE_NAME, [
            'id' => $this->primaryKey(),
            'pid' => $this->integer()->notNull()->defaultValue(0)->comment('父ID'),
            'sort' => $this->integer()->notNull()->defaultValue(100)->comment('排序'),
            'name' => $this->string(50)->notNull()->comment('名称'),
            'icon' => $this->string(50)->notNull()->defaultValue('')->comment('图标'),
            'category' => $this->string(50)->notNull()->comment('种类, MENU:菜单 FN:功能'),
            'link' => $this->string(191)->notNull()->unique()->comment('链接'),
            'dump_way' => $this->string(50)->notNull()->defaultValue('_self')->comment('链接方式, _blank:新窗口 _self:当前窗口 _parent:父窗口 _top:顶部窗口'),
            'desc' => $this->string()->notNull()->defaultValue('')->comment('描述'),
            'is_quick' => $this->boolean()->notNull()->defaultValue(0)->comment('是否允许设置为快捷操作, 0:不可以 1:可以; 请注意:只可未GET请求,不可动态传参,只能以弹窗方式打卡,请根据实际情况进行设置.'),
            'created_at' => $this->dateTime()->notNull(),
            'updated_at' => $this->dateTime()->defaultValue(null),
        ], $tableOptions);

        $this->createIndex('idx_pid', self::TABLE_NAME, 'pid');
        $this->createIndex('idx_category', self::TABLE_NAME, 'category');
        $this->createIndex('idx_is_quick', self::TABLE_NAME, 'is_quick');

        // 更新菜单
        $rbacManager = new RbacManager();
        $rbacManager->updateSystemMenu();
    }

    /**
     * {@inheritdoc}
     */
    public function down()
    {
        $this->dropTable(self::TABLE_NAME);
    }
}
